package com.tj.jinzao.manager;

import android.media.MediaRecorder;
import android.text.TextUtils;

import com.tj.jinzao.common.Constant;

import java.io.File;
import java.io.IOException;

/**
 * Created by wjy on 2015/11/29.
 * 语音录入管理
 */
public class AudioManager {
    private MediaRecorder mMediaRecorder;
    private String mCurrentFilePath;

    private static AudioManager sInstance;

    private boolean mPrepared;

    public static AudioManager getInstance() {
        if (sInstance == null) {
            synchronized (AudioManager.class) {
                if (sInstance == null) {
                    sInstance = new AudioManager();
                }
            }
        }
        return sInstance;
    }

    /**
     * 使用接口 用于回调
     */
    public interface AudioStateListener {
        void wellPrepared();
    }

    public AudioStateListener mAudioStateListener;

    /**
     * 回调方法
     */
    public void setOnAudioStateListener(AudioStateListener listener) {
        mAudioStateListener = listener;
    }

    /**
     * 准备语音
     */
    public void prepareAudio() {
        try {
            mPrepared = false;
            File dir = new File(Constant.SD_TEMP);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            //生成临时文件
            String fileName = System.currentTimeMillis() + ".amr";
            File file = new File(dir, fileName);

            mCurrentFilePath = file.getAbsolutePath();

            mMediaRecorder = new MediaRecorder();
            // 设置输出文件
            mMediaRecorder.setOutputFile(file.getAbsolutePath());
            // 设置MediaRecorder的音频源为麦克风
            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            // 设置音频格式
            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
            // 设置音频编码
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

            // 准备录音
            mMediaRecorder.prepare();
            // 开始
            mMediaRecorder.start();
            // 准备结束
            mPrepared = true;
            if (mAudioStateListener != null) {
                mAudioStateListener.wellPrepared();
            }

        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取当前录制声音等级
     *
     * @param maxLevel
     * @return
     */
    public int getVoiceLevel(int maxLevel) {
        if (mPrepared) {
            try {
                // mMediaRecorder.getMaxAmplitude() 1~32767
                return maxLevel * mMediaRecorder.getMaxAmplitude() / 32768 + 1;
            } catch (Exception e) {
            }
        }
        return 1;
    }

    /**
     * 释放资源
     */
    public void release() {
        //mMediaRecorder.stop();
        mMediaRecorder.reset();
        mMediaRecorder = null;
    }

    /**
     * 取消录音
     */
    public void cancel() {
        release();
        if (!TextUtils.isEmpty(mCurrentFilePath)) {
            File file = new File(mCurrentFilePath);
            file.delete();
            mCurrentFilePath = null;
        }

    }

    public String getCurrentFilePath() {
        return mCurrentFilePath;
    }
}